package com.losl.framework.web.service;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.losl.common.util.SpringUtils;
import com.losl.common.util.StringUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * 通用属性注入
 */
public class AutoDataInject implements MetaObjectHandler {
    private String createBy = "createBy";
    private String createTime = "createTime";
    private String updataBy = "updataBy";
    private String updataTime = "updataTime";

    @Override
    public void insertFill(MetaObject metaObject) {
        TokenService tokenService = SpringUtils.getBean(TokenService.class);
        if (StringUtils.isNull(getFieldValByName(createBy,metaObject))){
            strictInsertFill(metaObject,createBy,String.class,tokenService.getUserName());
        }
        if (StringUtils.isNull(getFieldValByName(createTime,metaObject))){
            strictInsertFill(metaObject,createTime, Date.class,new Date());
        }
        if (StringUtils.isNull(getFieldValByName(updataBy,metaObject))){
            strictInsertFill(metaObject,createBy,String.class,tokenService.getUserName());
        }
        if (StringUtils.isNull(getFieldValByName(updataTime,metaObject))){
            strictInsertFill(metaObject,createTime, Date.class,new Date());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        TokenService tokenService = SpringUtils.getBean(TokenService.class);
        if (StringUtils.isNull(getFieldValByName(updataBy,metaObject))){
            strictInsertFill(metaObject,createBy,String.class,tokenService.getUserName());
        }
        if (StringUtils.isNull(getFieldValByName(updataTime,metaObject))){
            strictInsertFill(metaObject,createTime, Date.class,new Date());
        }
    }
}
